package com.fgchen.controller;

import com.fgchen.biz.ExcelBiz;
import com.fgchen.biz.SummaryBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;

/**
 * Created by Administrator on 2018-04-08.
 */
//@RestController
@Controller
public class SummaryController {

    @Autowired
    private SummaryBiz summaryBiz;

    @ResponseBody
    @RequestMapping("/summary")
    public Map<String, Collection<?>> summary() {
        return this.summaryBiz.summary();
    }

    @RequestMapping("/export")
    public void excel(HttpServletResponse response) throws Exception {
        InputStream inputStream = this.summaryBiz.exportExcel();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=person.xls");
        OutputStream outputStream = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buf)) >= 0) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    }

}
